package cn.soxing.pojo;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * BaseUser entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.soxing.pojo.BaseUser
 * @author MyEclipse Persistence Tools
 */

public class BaseUserDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(BaseUserDAO.class);
	// property constants
	public static final String TYPE = "type";
	public static final String LOGIN_ID = "loginId";
	public static final String CITY = "city";
	public static final String USERNAME = "username";
	public static final String SEX = "sex";
	public static final String PASSWORD = "password";
	public static final String EMAIL = "email";
	public static final String TELEPHONE = "telephone";
	public static final String CERTIFICATETYPE = "certificatetype";
	public static final String CERTIFICATENUM = "certificatenum";

	protected void initDao() {
		// do nothing
	}

	public void save(BaseUser transientInstance) {
		log.debug("saving BaseUser instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(BaseUser persistentInstance) {
		log.debug("deleting BaseUser instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public BaseUser findById(java.lang.Integer id) {
		log.debug("getting BaseUser instance with id: " + id);
		try {
			BaseUser instance = (BaseUser) getHibernateTemplate().get(
					"cn.soxing.pojo.BaseUser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(BaseUser instance) {
		log.debug("finding BaseUser instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding BaseUser instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from BaseUser as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findByLoginId(Object loginId) {
		return findByProperty(LOGIN_ID, loginId);
	}

	public List findByCity(Object city) {
		return findByProperty(CITY, city);
	}

	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByTelephone(Object telephone) {
		return findByProperty(TELEPHONE, telephone);
	}

	public List findByCertificatetype(Object certificatetype) {
		return findByProperty(CERTIFICATETYPE, certificatetype);
	}

	public List findByCertificatenum(Object certificatenum) {
		return findByProperty(CERTIFICATENUM, certificatenum);
	}

	public List findAll() {
		log.debug("finding all BaseUser instances");
		try {
			String queryString = "from BaseUser";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public BaseUser merge(BaseUser detachedInstance) {
		log.debug("merging BaseUser instance");
		try {
			BaseUser result = (BaseUser) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(BaseUser instance) {
		log.debug("attaching dirty BaseUser instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(BaseUser instance) {
		log.debug("attaching clean BaseUser instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static BaseUserDAO getFromApplicationContext(ApplicationContext ctx) {
		return (BaseUserDAO) ctx.getBean("BaseUserDAO");
	}
}